package com.wyb.service.impl;

import com.wyb.dao.UserDao;
import com.wyb.dao.impl.UserDaoImpl;
import com.wyb.entity.User;
import com.wyb.service.UserService;
import com.wyb.util.JDBCUtils;

import java.sql.Connection;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public boolean login(String name, String password) {
        boolean b=false;

       User user=userDao.selectUserByName(name);
       if(user!=null){
           if(user.getPassword().equals(password)){
               b=true;
           }else {
               throw new RuntimeException("密码错误");
           }
       }else {
           throw new RuntimeException("用户名不存在");
       }
       return  b;
    }

    @Override
    public boolean register(User user) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(userDao.insertUser(user)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }
}
